﻿namespace ImageProcessing.ImageProcessor
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// normalize class
    /// </summary>
    public static class IMG_Normalize
    {
        /// <summary>
        /// normalize the image.
        /// </summary>
        /// <param name="imageOriginal">The image original.</param>
        /// <returns>normalized image</returns>
        public static Entities.IMG_Image IMG_NormalizeImage(Entities.IMG_Image imageOriginal)
        {
            try
            {
                int w = imageOriginal.Width;
                int h = imageOriginal.Height;
                double minR = double.MaxValue;
                double maxR = double.MinValue;
                double minG = double.MaxValue;
                double maxG = double.MinValue;
                double minB = double.MaxValue;
                double maxB = double.MinValue;
                Entities.IMG_Image retImg = new Entities.IMG_Image(w, h);
                int index = w * h;
                for (int i = 0; i < index; i++)
                {
                    minR = minR < imageOriginal.ImagePixels[i].Red ? minR : imageOriginal.ImagePixels[i].Red;
                    maxR = maxR > imageOriginal.ImagePixels[i].Red ? maxR : imageOriginal.ImagePixels[i].Red;
                    minG = minG < imageOriginal.ImagePixels[i].Green ? minG : imageOriginal.ImagePixels[i].Green;
                    maxG = maxG > imageOriginal.ImagePixels[i].Green ? maxG : imageOriginal.ImagePixels[i].Green;
                    minB = minB < imageOriginal.ImagePixels[i].Blue ? minB : imageOriginal.ImagePixels[i].Blue;
                    maxB = maxB > imageOriginal.ImagePixels[i].Blue ? maxB : imageOriginal.ImagePixels[i].Blue;
                }

                double red, green, blue;
                for (int i = 0; i < index; i++)
                {
                    if (maxR == minR && minR == 0)
                    {
                        red = 0;
                    }
                    else
                    {
                        red = ((imageOriginal.ImagePixels[i].Red - minR) / (maxR - minR)) * 255;
                    }

                    if (maxG == minG && minG == 0)
                    {
                        green = 0;
                    }
                    else
                    {
                        green = ((imageOriginal.ImagePixels[i].Green - minG) / (maxG - minG)) * 255;
                    }

                    if (maxB == minB && minB == 0)
                    {
                        blue = 0;
                    }
                    else
                    {
                        blue = ((imageOriginal.ImagePixels[i].Blue - minB) / (maxB - minB)) * 255;
                    }

                    retImg.ImagePixels[i] = new Entities.Pixel(red, green, blue);
                }

                return retImg;
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                throw;
            }
        }
    }
}
